package com.note.simpleadmin.module.shop.controller;
import com.note.simpleadmin.constant.ErrorCode;
import com.note.simpleadmin.dto.SpecDto;
import com.note.simpleadmin.entity.TbSpec;
import com.note.simpleadmin.exception.DataNotExistException;
import com.note.simpleadmin.module.shop.service.SpecService;
import com.note.simpleadmin.module.shop.vo.SpecVo;
import com.note.simpleadmin.vo.Msg;
import com.note.simpleadmin.vo.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/***
 *@ClaseName:BrandController
 *
 *@Decription:
 *
 *@Author: xubingbing
 *
 *@Date:2019/1/31 0031 20:58
 */
@Controller
@RequestMapping(value = "shop/spec")
public class SpecController {
    private final Logger logger = LoggerFactory.getLogger(SpecController.class);
    @Autowired
    private SpecService specService;
    @RequestMapping(value = "index")
    public String index() {
        return "shop/goods/spec";
    }
    @RequestMapping(value = "ajax_list")
    @ResponseBody
    public Msg ajax_list(String code, Integer page)
    {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            PageBean pageBean =specService.findByPage(code,page);
            msg.setData(pageBean);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "all")
    @ResponseBody
    public Msg ajax_all()
    {
        Msg msg=new Msg();
        try {
            msg.setErrorCode(ErrorCode.ERROR_OK);
            List<SpecVo> specList=specService.findAll();
            msg.setData(specList);
            msg.setMsg("加载完成");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setMsg("系统异常");
            msg.setErrorCode(ErrorCode.ERROR_1001);
        }
        return msg;
    }
    @RequestMapping(value = "addSpec")
    public String addSpec()
    {
        return "shop/goods/addSpec";
    }
    @RequestMapping(value = "editSpec")
    public String editSpec()
    {
        return "shop/goods/editSpec";
    }
    @RequestMapping(value = "ajax_detail")
    @ResponseBody
    public Msg ajax_detail(String id)
    {
        Msg msg=new Msg();
        try {
            SpecVo specVo=specService.getOne(id);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setData(specVo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_add")
    @ResponseBody
    public Msg ajax_add(@RequestBody  SpecDto specDto)
    {
        Msg msg=new Msg();
        try {
            specService.save(specDto);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("添加成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_update")
    @ResponseBody
    public Msg ajax_update(@RequestBody  SpecDto specDto)
    {
        Msg msg=new Msg();
        try {
            specService.update(specDto);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("更新成功");
        } catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
    @RequestMapping(value = "ajax_del")
    @ResponseBody
    public Msg ajax_del(String id)
    {
        Msg msg=new Msg();
        try {
            specService.delete(id);
            msg.setErrorCode(ErrorCode.ERROR_OK);
            msg.setMsg("删除成功");
        }catch (DataNotExistException e)
        {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1000);
            msg.setMsg(e.getMessage());
        }catch (Exception e) {
            logger.error(e.getMessage());
            msg.setErrorCode(ErrorCode.ERROR_1001);
            msg.setMsg("系统异常");
        }
        return msg;
    }
}
